package com.carl.lambda.streamAPI.lambda01;


import com.carl.lambda.simpleLambda.lambda1.Employee;
import org.junit.Test;


import java.util.*;

/*
 * 一、Stream API 的操作步骤：
 *
 * 1. 创建 Stream
 *
 * 2. 中间操作
 *
 * 3. 终止操作(终端操作)
 */
public class TestStreamAPI3 {


    List<Employee> employees = Arrays.asList(
            new Employee("张三",18,99.99, Employee.Status.BUSY),
            new Employee("李四",28,999.99, Employee.Status.FREE),
            new Employee("王五",32,9999.99, Employee.Status.VOCATION),
            new Employee("赵六",48,9999.99,Employee.Status.BUSY),
            new Employee("钱七",58,9999.99,Employee.Status.VOCATION),
            new Employee("钱七",58,9999.99,Employee.Status.FREE),
            new Employee("钱七",58,9999.99, Employee.Status.BUSY)
    );
    //3. 终止操作
	/*
		allMatch——检查是否匹配所有元素
		anyMatch——检查是否至少匹配一个元素
		noneMatch——检查是否没有匹配的元素
		findFirst——返回第一个元素
		findAny——返回当前流中的任意元素
		count——返回流中元素的总个数
		max——返回流中最大值
		min——返回流中最小值
	 */

    @Test
    public void test02(){
        Optional<Employee> optionalEmployee = employees.stream().max((x, y) -> Double.compare(x.getSalary(), y.getSalary()));
        Employee employee = optionalEmployee.get();
        System.out.println(employee);

        long count = employees.size();
        System.out.println(count);

        //返回工资的最小值只要工资
        Optional<Double> min = employees.stream().map(Employee::getSalary)
                .min(Double::compare);
        Double v = min.get();
        System.out.println(v);


    }
    @Test
    public void test01(){
        boolean b = employees.stream().allMatch(e -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b);


        ////////////////////////////
        boolean b1 = employees.stream().anyMatch(e -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b1);

        ////////////////////////////
        boolean b2 = employees.stream().noneMatch(e -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b2);

        ////////////////////////////
        Employee employee = employees.stream().sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
                .findFirst().get();
        System.out.println(employee);

        ////////////////////////////
        Employee employee1 = employees.stream().filter(e -> e.getStatus().equals(Employee.Status.FREE)).findAny().get();
        System.out.println(employee1);



    }




















}
